home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / c / strtol < prev    next >
Text File  |  1996-11-09  |  2KB  |  130 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/c/RCS/strtol,v $
  4.  * $Date: 1996/04/19 21:26:42 $
  5.  * $Revision: 1.1 $
  6.  * $State: Rel $
  7.  * $Author: simon $
  8.  *
  9.  * $Log: strtol,v $
  10.  * Revision 1.1  1996/04/19 21:26:42  simon
  11.  * Initial revision
  12.  *
  13.  ***************************************************************************/
  14.  
  15. static const char rcs_id[] = "$Id: strtol,v 1.1 1996/04/19 21:26:42 simon Rel $";
  16.  
  17. #include <ctype.h>
  18. #include <stdlib.h>
  19.  
  20. int
  21.   (atoi) (register const char *s)
  22.  
  23. {
  24.   return (atoi (s));
  25. }
  26.  
  27. long
  28.   (atol) (register const char *s)
  29.  
  30. {
  31.   return (atol (s));
  32. }
  33.  
  34. #define digit(x)    (((x) > '9') ? (((x) & 31) + 9) : ((x) - '0'))
  35.  
  36. long
  37. strtol (register const char *s, char **end, register int b)
  38.  
  39. {
  40.   register long r = 0L;
  41.   register int r_ = 0, d;
  42.  
  43.   if (!s)
  44.     return (r);
  45.  
  46.   while (isspace (*s))
  47.     s++;
  48.  
  49.   if (*s == '+' || *s == '-')
  50.     {
  51.       if (*s == '-')
  52.     r_ = 1;
  53.       s++;
  54.     }
  55.  
  56.   if (!b)
  57.     {
  58.       if (*s == '0')
  59.     {
  60.       s++;
  61.       b = 010;
  62.       if (*s == 'x')
  63.         {
  64.           s++;
  65.           b = 0x10;
  66.         }
  67.     }
  68.       else
  69.     b = 10;
  70.     }
  71.   else if (b == 16)
  72.     if (*s == '0' && *++s == 'x')
  73.       s++;
  74.  
  75.   while (d = digit (*s), d >= 0 && d < b)
  76.     {
  77.       r = r * (long) b + (long) d;
  78.       s++;
  79.     }
  80.  
  81.   if (end)
  82.     *end = (char *) s;
  83.  
  84.   return (r_ ? -r : r);
  85. }
  86.  
  87. unsigned long
  88. strtoul (register const char *s, char **end, register int b)
  89.  
  90. {
  91.   register unsigned long r = 0;
  92.   register int d;
  93.  
  94.   if (!s)
  95.     return (r);
  96.  
  97.   while (isspace (*s))
  98.     s++;
  99.  
  100.   if (!b)
  101.     {
  102.       if (*s == '0')
  103.     {
  104.       s++;
  105.       b = 010;
  106.       if (*s == 'x')
  107.         {
  108.           s++;
  109.           b = 0x10;
  110.         }
  111.     }
  112.       else
  113.     b = 10;
  114.     }
  115.   else if (b == 16)
  116.     if (*s == '0' && *++s == 'x')
  117.       s++;
  118.  
  119.   while (d = digit (*s), d >= 0 && d < b)
  120.     {
  121.       r = r * (unsigned long) b + (unsigned long) d;
  122.       s++;
  123.     }
  124.  
  125.   if (end)
  126.     *end = (char *) s;
  127.  
  128.   return (r);
  129. }
  130.